package com.example.communitypro.service.impl;

import com.example.communitypro.mapper.LabelMapper;
import com.example.communitypro.mapper.LabelRelationMapper;
import com.example.communitypro.pojo.Label;
import com.example.communitypro.pojo.Post;
import com.example.communitypro.service.LabelService;
import com.example.communitypro.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LabelServiceImpl implements LabelService {
    @Autowired(required=false)
    private LabelMapper labelMapper;
    @Autowired(required=false)
    private LabelRelationMapper labelRelationMapper;
    @Override
    public Result addLabel(Label record) {
        try {
            Label label = labelMapper.judgeLabel(record.getLabelName());
            if(label==null)
            {
                record.setLabelHot(0);
                labelMapper.insertSelective(record);
                Label label1 = labelMapper.judgeLabel(record.getLabelName());
                return Result.ok().message("新增标签成功").data("label1",label1);
            }
            else{
                return Result.error().message("新增标签失败，已经存在").data("label1",label);
            }
        }catch (Exception e){
            return Result.error().message("新增标签失败");
        }
    }

    @Override
    public Result dropLabel(Integer labelNo) {
        try {
            if(labelMapper.selectByPrimaryKey(labelNo)!=null)
            {
                labelRelationMapper.deleteByLabelNo(labelNo);
                labelMapper.deleteByPrimaryKey(labelNo);
                return Result.ok().message("删除标签成功");
            }
            else{
                return Result.error().message("删除标签失败");
            }
        }catch (Exception e){
            return Result.error().message("删除标签失败");
        }
    }

    @Override
    public Result queryLabel(Integer labelNo) {
        try {
            Label label=labelMapper.selectByPrimaryKey(labelNo);
            if(label!=null)
            {
                return Result.ok().message("查找标签成功").data("label",label);
            }
            else{
                return Result.error().message("查找标签失败");
            }
        }catch (Exception e){
            return Result.error().message("查找标签失败");
        }
    }

    @Override
    public Result modifyLabel(Label record) {
        try {
            if(labelMapper.updateByPrimaryKeySelective(record)>0)
            {
                return Result.ok().message("修改成功");
            }
            else{
                return Result.error().message("修改失败：未找到对应记录");
            }
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("修改失败：已经存在对应记录");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("修改失败："+e.toString());
        }
    }

    @Override
    public Result findAllLabel() {
        try {
            List<Label> labels=labelMapper.selectByHot();
            if(labels!=null)
            {
                return Result.ok().message("找到所有标签成功").data("labels",labels);
            }
            else{
                return Result.error().message("找到所有标签失败");
            }
        }catch (Exception e){
            return Result.error().message("找到所有标签失败");
        }
    }

    @Override
    public Result findLabelByRand() {
        try {
            List<Label> labels=labelMapper.selectByRand();
            if(labels!=null)
            {
                return Result.ok().message("随机标签成功").data("labels",labels);
            }
            else{
                return Result.error().message("随机标签失败");
            }
        }catch (Exception e){
            return Result.error().message("随机标签失败");
        }
    }

}
